package com.ruoyi.lab.service.impl;

import java.util.*;

import com.ruoyi.lab.domain.JacTestResult;
import com.ruoyi.lab.mapper.JacTestProjectMapper;
import com.ruoyi.lab.mapper.JacTestResultMapper;
import com.ruoyi.lab.service.IJacTestResultService;
import com.ruoyi.lab.utils.TimeTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.core.text.Convert;

/**
 * testService业务层处理
 * author sxx
 * date 2021-04-06
 */

@Service
public class JacTestResultServiceImpl implements IJacTestResultService
{
    @Autowired
    private JacTestResultMapper jacTestResultMapper;
    @Autowired
    private JacTestProjectMapper jacTestProjectMapper;
    private static final String OK="1";
    /**
     * 更据code查询result
     */
    @Override
    public List<JacTestResult> selectJacTestResultById(Long code)
    {
        return jacTestResultMapper.selectJacTestResultById(code);
    }


    /**
     * 查询test列表
     * 当result有不合格时即为不合格
     *
     */

    @Override
    public Map<String, Object> selectJacTestResultList(JacTestResult jacTestResult)
    {  HashMap<String,Object> message=new HashMap<>();
        message.put("datas", jacTestResultMapper.selectJacTestResultList(jacTestResult));
        String t="success";
        List<JacTestResult> jacTestResultList=  jacTestResultMapper.selectJacTestResultListNew(jacTestResult.getCode());
        if(jacTestResultList.size()==0) {
            t="notrecorded";
            message.put("result",t);
            message.put("msg", "success");
            message.put("code", "0");
            return message;
        }
        for (JacTestResult jacTestResult1:jacTestResultList) {
            if("fault".equals(jacTestResult1.getExperimentresult())) {
                t="fault";
                break;
            }
        }
        message.put("result",t);
        message.put("msg", "success");
        message.put("code", "0");
        return message;
    }



    /**
     * 新增result
     */


    @Override
        public String insertJacTestResult(JacTestResult jacTestResult)
    {
        String result=OK;
          if("complete".equals(jacTestProjectMapper.selectJacTestProjectById(jacTestResult.getCode()).getExperimentstatus())){
            result="计划已完成，不允许录入实验结果";
          }else {
              Date date=new Date();
              jacTestResult.setCreatetime(TimeTool.DateToStringDay(date));
              jacTestResultMapper.insertJacTestResult(jacTestResult);
              }
           return result;
    }



    /**
     * 修改result
     */

    @Override
    public int updateJacTestResult(JacTestResult jacTestResult)
    {
        return jacTestResultMapper.updateJacTestResult(jacTestResult);
    }


    /**
     * 批量删除result
     */


    @Override
    public int deleteJacTestResultByIds(String ids)
    {
        return jacTestResultMapper.deleteJacTestResultByIds(Convert.toStrArray(ids));
    }


    /**
     * 单条删除result
     */
    public int deleteJacTestResultById(Long code)
    {
        return jacTestResultMapper.deleteJacTestResultById(code);
    }
}
